package net.neptech.listener.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import net.neptech.model.dto.InstanceAssignUserRecordParamDto;
import net.neptech.enums.workflow.InstanceConstant;
import net.neptech.listener.AbstractEventListenerStrategy;
import net.neptech.service.RemoteService;
import net.neptech.util.FlowableUtil;
import net.neptech.util.JsonUtil;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 任务完成
 */
@Slf4j
@Component
public class TaskCompletedEventListener extends AbstractEventListenerStrategy implements InitializingBean {
    @Autowired
    private RemoteService remoteService;
    /**
     * 处理数据
     */
    @Override
    public void handle(FlowableEvent event) {

        TaskService taskService = SpringUtil.getBean(TaskService.class);
        RuntimeService runtimeService = SpringUtil.getBean(RuntimeService.class);

        //任务完成
        FlowableEntityEvent flowableEntityEvent = (FlowableEntityEvent) event;
        TaskEntityImpl task = (TaskEntityImpl) flowableEntityEvent.getEntity();
        //执行人id
        String assignee = task.getAssignee();
        //nodeid
        String nodeId = task.getTaskDefinitionKey();
        //实例id
        String processInstanceId = task.getProcessInstanceId();
        String processDefinitionId = task.getProcessDefinitionId();
        //流程id
        String tenantId = task.getTenantId();
        String flowId = FlowableUtil.getFlowId(processDefinitionId);
        InstanceAssignUserRecordParamDto instanceAssignUserRecordParamDto = new InstanceAssignUserRecordParamDto();
        instanceAssignUserRecordParamDto.setFlowId(flowId);
        instanceAssignUserRecordParamDto.setProcessInstanceId(processInstanceId);
        //   processNodeRecordAssignUserParamDto.setParentExecutionId();
        Map<String, Object> variables = taskService.getVariables(task.getId());
        instanceAssignUserRecordParamDto.setData(JsonUtil.toJSONString(variables));
        instanceAssignUserRecordParamDto.setLocalData(JsonUtil.toJSONString(taskService.getVariablesLocal(task.getId())));
        instanceAssignUserRecordParamDto.setNodeId(nodeId);
        instanceAssignUserRecordParamDto.setUserId(assignee);
        instanceAssignUserRecordParamDto.setTaskId(task.getId());
        instanceAssignUserRecordParamDto.setNodeName(task.getName());
        instanceAssignUserRecordParamDto.setFlowUniqueId(FlowableUtil.getFlowUniqueId(nodeId, flowId,
                processInstanceId, tenantId));
        String taskType = task.getVariableLocal(InstanceConstant.VariableKey.TASK_TYPE, String.class);
        instanceAssignUserRecordParamDto.setTaskType(taskType);
        instanceAssignUserRecordParamDto.setExecutionId(task.getExecutionId());
        //审批结果
        //true同意  false拒绝
        //判断下 如果是自动完成的 不处理
        Object autoCompleteObj = runtimeService.getVariableLocal(task.getExecutionId(),
                InstanceConstant.VariableKey.AUTO_COMPLETE_TASK);
        instanceAssignUserRecordParamDto.setAuto(Convert.toBool(autoCompleteObj, false));
        remoteService.taskCompletedEvent(instanceAssignUserRecordParamDto);
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        afterPropertiesSet(FlowableEngineEventType.TASK_COMPLETED.toString());
    }
}
